package com.xinxin.intelligenceexam.model;

import com.orhanobut.logger.Logger;
import com.xinxin.intelligenceexam.api.ApiException;
import com.xinxin.intelligenceexam.api.Generator;
import com.xinxin.intelligenceexam.api.JavaExamApi;
import com.xinxin.intelligenceexam.contract.LoginContract;
import com.xinxin.intelligenceexam.domain.HttpResult;
import com.xinxin.intelligenceexam.domain.Student;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by x9259 on 2016/10/26
 */

public class LoginModelImpl implements LoginContract.Model {

    @Override
    public void login(Subscriber<Student> subscriber, String userCode, String password) {
        Observable<HttpResult<Student>> studentObservable = Generator.createService(JavaExamApi.class).studentLogin(userCode, password);
        studentObservable
                .map(new Func1<HttpResult<Student>, Student>() {
                    @Override
                    public Student call(HttpResult<Student> aReturn) {
                        if (aReturn.getStatus() == 1) {
                            Logger.d(aReturn.getData());
                            return aReturn.getData();
                        }
                        throw new ApiException("未获取到内容");
                    }
                })
                .subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    @Override
    public boolean saveUserInfo() {
        return false;
    }
}